package com.example.jl.bethinner.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.bethinner.demo.InputString;
import com.bethinner.utils.InputStringDaoHelper;
import com.example.jl.bethinner.R;
import com.example.jl.bethinner.base.BaseActivity;

import java.util.ArrayList;
import java.util.List;

public class AutoCompleteTextViewActivity extends BaseActivity {

    private AutoCompleteTextView autoCompleteTextView;
    private Button button;
    private InputStringDaoHelper inputStringDaoHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_auto_complete_text_view);
        //已输入的StringHelper
        inputStringDaoHelper = InputStringDaoHelper.getInstance(this);

        autoCompleteTextView=(AutoCompleteTextView)findViewById(R.id.auto_complete_text);
        button=(Button)findViewById(R.id.button);
        //创建一个ArrayAdapter
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_dropdown_item_1line,getData());
        //将AutoCompleteTextView与ArrayAdapter进行绑定
        autoCompleteTextView.setAdapter(adapter);
        //设置AutoCompleteTextView输入1个字符就进行提示
        autoCompleteTextView.setThreshold(1);
        autoCompleteTextView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ListView listview = (ListView) parent;
                ArrayAdapter<String> adapter = (ArrayAdapter<String>) parent.getAdapter();
                TextView textview = (TextView) view;
                Toast.makeText(AutoCompleteTextViewActivity.this,"-->"+ textview.getText().toString(),Toast.LENGTH_SHORT).show();
            }
        });
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                InputString _inputString = new InputString();
                _inputString.setInString(autoCompleteTextView.getText().toString());
                inputStringDaoHelper.addData(_inputString);
            }
        });
    }
    private List<String> getData(){
        List<String> data = new ArrayList<String>();
        for (InputString inputString: inputStringDaoHelper.getAllData()){
            data.add(inputString.getInString());
        }
        return data;
    }
}
